#coding:utf-8
from django.http import HttpResponseRedirect, HttpResponse
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from lovewith.account.views.modules.weddingapi import WeddingApi
from lovewith.views import site_info
from lovewith.card.views.models.card_api import CardApi
from lovewith.account.views.modules.userapi import UserApi


#请柬主页
def card_home(request):
    card_data = None
    if request.user.is_authenticated():
        card_data = CardApi.check_data(request.user.id)

    return render_to_response('card/index.jinja', {
        'card_data': card_data
    }, context_instance=RequestContext(request, processors=[site_info]))


#请柬编辑页面
@login_required
def card_setting(request):
    user_auth = UserApi.get_auth(request.user.id)
    card_post = []
    invite_data = []

    #请柬设置数据
    card_data = CardApi.check_data(request.user.id)
    if card_data:
        #获取请柬内容数据
        card_post = CardApi.get_postdata_by_card(card_data.id)
        #已经邀请的人
        invite_data = CardApi.get_invite_user(card_data.id)

    wedding_data = WeddingApi.check_data(request.user.id)

    return render_to_response('card/setting.jinja', {
        'user_auth': user_auth,
        'card_data': card_data,
        'card_post': card_post,
        'invite_data': invite_data,
        'wedding_data':wedding_data
    }, context_instance=RequestContext(request, processors=[site_info]))


#用户请柬前台页面
def show_card(request, card_id):
    #请柬设置数据
    card_data = CardApi.check_card(card_id)
    if not card_data:
        return HttpResponseRedirect('/')

    #是否被邀请
    be_invited = False
    #邀请语
    invite_text = ''
    #显示的邀请名
    invited_name = ''
    #是否有查看的权限
    has_permission = False
    open_txt = ''

    invite_email = request.GET.get('mail')
    #其它来源
    orign = request.GET.get('type')

    if request.user.is_authenticated():
        #自己的请柬
        if card_data.user_id == request.user.id:
            be_invited = True
            invited_name = ''

        #根据email查询
        if not be_invited:
            if request.user.email:
                invite_filter_data = CardApi.have_invited(card_id, email=request.user.email)
                if invite_filter_data:
                    be_invited = True
                    invited_name = invite_filter_data.user_name

            #根据第三方网站app_id查询
            if request.user.app_uid:
                invite_filter_data = CardApi.have_invited(card_id, app_uid=request.user.app_uid)
                if invite_filter_data:
                    be_invited = True
                    invited_name = invite_filter_data.user_name
    else:
        if invite_email:
            #判断邮件邀请是否存在
            invite_filter_data = CardApi.have_invited(card_id, email=invite_email)
            if invite_filter_data:
                be_invited = True
                invited_name = invite_filter_data.user_name

    if be_invited:
        has_permission = True
        invite_text = u'您收到了%s' % card_data.title
        open_txt = u'点击打开请柬'
    else:
        invite_text = u'您还没有被邀请哦~'

        if card_data.permission == 2:
            open_txt = u'该请柬不公开'
        else:
            has_permission = True
            open_txt = u'继续打开请柬'
    #婚礼数据
    wedding_data = WeddingApi.check_data(card_data.user_id)

    #没有登录并且来源为微博或者QQ
    if not request.user.is_authenticated() and orign:
        return render_to_response('card/card-sign.jinja', {
            'card_data': card_data,
            'wedding_data': wedding_data,
            'orign': orign
        }, context_instance=RequestContext(request, processors=[site_info]))
    else:
        #获取请柬内容数据
        card_post = CardApi.get_postdata_by_card(card_data.id)

        return render_to_response('card/card.jinja', {
            'card_data': card_data,
            'has_permission': has_permission,
            'card_post': card_post,
            'invited_name': invited_name,
            'invite_text': invite_text,
            'open_txt': open_txt,
            'wedding_data': wedding_data,
        }, context_instance=RequestContext(request, processors=[site_info]))


#测试环境下提供flash跨域
def crossdomain(requewst):
    html = '<?xml version="1.0"?><cross-domain-policy><allow-access-from domain="*" /></cross-domain-policy>'
    return HttpResponse(html, mimetype="application/xml")
